<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * 模型基础类
 */
class BaseModel extends Model
{
    /**
     * 是否开启自动写入时间戳
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 创建时间字段
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间字段
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除字段
     * @var string
     */
    protected $deleteTime = 'delete_time';

    /**
     * 字段映射配置
     * @var array
     */
    protected $schema = [];

    /**
     * 关联模型配置
     * @var array
     */
    protected $relations = [];

    /**
     * 验证规则
     * @var array
     */
    protected $rules = [];

    /**
     * 验证消息
     * @var array
     */
    protected $messages = [];

    /**
     * 获取列表数据
     * @param array $params 查询参数
     * @param array $with 关联查询
     * @return array
     */
    public function getList(array $params = [], array $with = []): array
    {
        $query = $this;
        
        // 处理关联查询
        if (!empty($with)) {
            $query = $query->with($with);
        }
        
        // 处理查询条件
        if (!empty($params['where']) && is_array($params['where'])) {
            foreach ($params['where'] as $field => $value) {
                if (is_array($value) && count($value) >= 2) {
                    $query = $query->where($field, $value[0], $value[1]);
                } else {
                    $query = $query->where($field, $value);
                }
            }
        }
        
        // 处理模糊查询
        if (!empty($params['like']) && is_array($params['like'])) {
            foreach ($params['like'] as $field => $value) {
                $query = $query->whereLike($field, '%' . $value . '%');
            }
        }
        
        // 处理时间范围查询
        if (!empty($params['time']) && is_array($params['time'])) {
            foreach ($params['time'] as $field => $value) {
                if (is_array($value) && count($value) >= 2) {
                    $query = $query->whereTime($field, 'between', $value);
                }
            }
        }
        
        // 处理排序
        if (!empty($params['order']) && is_array($params['order'])) {
            foreach ($params['order'] as $field => $type) {
                $query = $query->order($field, $type);
            }
        }
        
        // 处理分页
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 15;
        
        $total = $query->count();
        $items = $query->page($page, $limit)->select();
        
        return [
            'total' => $total,
            'items' => $items,
            'page' => intval($page),
            'limit' => intval($limit),
            'pages' => ceil($total / $limit)
        ];
    }
    
    /**
     * 获取单条数据
     * @param mixed $id 主键值
     * @param array $with 关联查询
     * @return array|null
     */
    public function getInfo($id, array $with = [])
    {
        $query = $this;
        
        // 处理关联查询
        if (!empty($with)) {
            $query = $query->with($with);
        }
        
        return $query->find($id);
    }
    
    /**
     * 创建数据
     * @param array $data 数据
     * @return mixed
     */
    public function createData(array $data)
    {
        return $this->create($data);
    }
    
    /**
     * 更新数据
     * @param array $data 更新的数据
     * @param string|null $sequence 序列名
     * @return bool
     */
    public function updateData(array $data, string $sequence = null): bool
    {
        return $this->save($data, $sequence);
    }
    
    /**
     * 删除数据
     * @param mixed $id 主键值
     * @return mixed
     */
    public function deleteData($id)
    {
        return $this->destroy($id);
    }
    
    /**
     * 批量删除数据
     * @param array $ids 主键数组
     * @return mixed
     */
    public function batchDelete(array $ids)
    {
        return $this->destroy($ids);
    }
    
    /**
     * 获取验证规则
     * @return array
     */
    public function getRules(): array
    {
        return $this->rules;
    }
    
    /**
     * 获取验证消息
     * @return array
     */
    public function getMessages(): array
    {
        return $this->messages;
    }
}